<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="老师ID" prop="teacher_id">
      <el-input v-model="dataForm.teacher_id" placeholder="老师ID"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="create_time">
      <el-date-picker type="date" placeholder="创建日期" v-model="dataForm.create_time" style="width: 100%;"></el-date-picker>
    </el-form-item>
    <el-form-item label="参加人数" prop="jointimes">
      <el-input v-model="dataForm.jointimes" placeholder="参加人数"></el-input>
    </el-form-item>
    <el-form-item label="开课时间" prop="start_time">
      <el-date-picker type="date" placeholder="开课时间" v-model="dataForm.start_time" style="width: 100%;"></el-date-picker>
    </el-form-item>
    <el-form-item label="结课时间" prop="end_time">
      <el-date-picker type="date" placeholder="结课时间" v-model="dataForm.end_time" style="width: 100%;"></el-date-picker>
    </el-form-item>
    <el-form-item label="课程描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="课程描述"></el-input>
    </el-form-item>
    <el-form-item label="课程目标" prop="goal">
      <el-input v-model="dataForm.goal" placeholder="课程目标"></el-input>
    </el-form-item>
    <el-form-item label="课程大纲" prop="outline">
      <el-input v-model="dataForm.outline" placeholder="课程大纲"></el-input>
    </el-form-item>
    <el-form-item label="课程价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="课程价格"></el-input>
    </el-form-item>
<!--    <el-form-item label="课程封面图" prop="coursePicture">-->
<!--      <el-input v-model="dataForm.course_picture" placeholder="课程封面图"></el-input>-->
<!--    </el-form-item>-->
      <el-upload
        class="avatar-uploader"
        action="http://127.0.0.1:8081/backstage/course/uploadImage"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    <el-form-item label="类型难度" prop="course_level">
      <el-input v-model="dataForm.course_level" placeholder="类型难度"></el-input>
    </el-form-item>
    <el-form-item label="老师姓名" prop="teachername">
      <el-input v-model="dataForm.teachername" placeholder="老师姓名"></el-input>
    </el-form-item>
    <el-form-item label="方向ID" prop="area_id">
      <el-input v-model="dataForm.area_id" placeholder="方向ID"></el-input>
    </el-form-item>
    <el-form-item label="分类ID" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="分类ID"></el-input>
    </el-form-item>
    <el-form-item label="总时长" prop="total_duration">
      <el-input v-model="dataForm.total_duration" placeholder="总时长"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        imageUrl: '',
        dataForm: {
          id: 0,
          title: '',
          teacher_id: '',
          create_time: '',
          jointimes: '',
          start_time: '',
          end_time: '',
          description: '',
          goal: '',
          outline: '',
          price: '',
          course_picture: '',
          course_level: '',
          teachername: '',
          cid: '',
          area_id: '',
          total_duration: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          teacher_id: [
            { required: true, message: '老师ID不能为空', trigger: 'blur' }
          ],
          create_time: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          jointimes: [
            { required: true, message: '参加人数不能为空', trigger: 'blur' }
          ],
          start_time: [
            { required: true, message: '开课时间不能为空', trigger: 'blur' }
          ],
          end_time: [
            { required: true, message: '结课时间不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '课程描述不能为空', trigger: 'blur' }
          ],
          goal: [
            { required: true, message: '课程目标不能为空', trigger: 'blur' }
          ],
          outline: [
            { required: true, message: '课程大纲不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '课程价格不能为空', trigger: 'blur' }
          ],
          course_picture: [
            { required: true, message: '课程封面图不能为空', trigger: 'blur' }
          ],
          course_level: [
            { required: true, message: '课程级别不能为空', trigger: 'blur' }
          ],
          teachername: [
            { required: true, message: '老师姓名不能为空', trigger: 'blur' }
          ],
          area_id: [
            { required: true, message: '方向ID不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '分类ID不能为空', trigger: 'blur' }
          ],
          total_duration: [
            { required: true, message: '总时长不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/course/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.course.title
                this.dataForm.teacher_id = data.course.teacher_id
                this.dataForm.create_time = data.course.create_time
                this.dataForm.jointimes = data.course.jointimes
                this.dataForm.start_time = data.course.start_time
                this.dataForm.end_time = data.course.end_time
                this.dataForm.description = data.course.description
                this.dataForm.goal = data.course.goal
                this.dataForm.outline = data.course.outline
                this.dataForm.price = data.course.price
                this.dataForm.course_picture = data.course.course_picture
                this.dataForm.course_level = data.course.course_level
                this.dataForm.teachername = data.course.teachername
                this.dataForm.cid = data.course.cid
                this.dataForm.area_id = data.course.area_id
                this.dataForm.total_duration = data.course.total_duration
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/course/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'teacher_id': this.dataForm.teacher_id,
                'create_time': this.dataForm.create_time,
                'jointimes': this.dataForm.jointimes,
                'start_time': this.dataForm.start_time,
                'end_time': this.dataForm.end_time,
                'description': this.dataForm.description,
                'goal': this.dataForm.goal,
                'outline': this.dataForm.outline,
                'price': this.dataForm.price,
                'course_picture': this.dataForm.course_picture,
                'course_level': this.dataForm.course_level,
                'teachername': this.dataForm.teachername,
                'cid': this.dataForm.cid,
                'area_id': this.dataForm.area_id,
                'total_duration': this.dataForm.total_duration
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleAvatarSuccess (res, file) {
        console.log(res)
        this.dataForm.course_picture = res.data
        this.imageUrl = res.data
        // this.imageUrl = URL.createObjectURL(file.raw)
      },
      beforeAvatarUpload (file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2

        // console.log(file)
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      }
    }
  }
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  margin-left: 300px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 70px;
  height: 70px;
  line-height: 70px;
  text-align: center;
}
.avatar {
  width: 70px;
  height: 70px;
  display: block;
}
</style>

